package o;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.RectF;
import android.os.Build.VERSION;
import android.text.Layout.Alignment;
import android.text.StaticLayout;
import android.text.StaticLayout.Builder;
import android.text.TextDirectionHeuristic;
import android.text.TextDirectionHeuristics;
import android.text.TextPaint;
import android.text.method.TransformationMethod;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.d;
import androidx.appcompat.widget.ActivityChooserView.ActivityChooserViewAdapter;
import androidx.appcompat.widget.AppCompatEditText;
import com.huawei.appgallery.festivalanimation.particle.modifiers.SizeModifier;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import o.ah.g;

class ci {
    private static final RectF ˎ = new RectF();
    private static ConcurrentHashMap<String, Method> ˏ = new ConcurrentHashMap();
    private boolean ʻ = false;
    private float ʼ = SizeModifier.STABLE_STATE_SCALE;
    private TextPaint ʽ;
    private boolean ˊ = false;
    private int ˋ = 0;
    private final TextView ˏॱ;
    private float ॱ = SizeModifier.STABLE_STATE_SCALE;
    private final Context ॱˊ;
    private float ॱॱ = SizeModifier.STABLE_STATE_SCALE;
    private int[] ᐝ = new int[0];

    ci(TextView textView) {
        this.ˏॱ = textView;
        this.ॱˊ = this.ˏॱ.getContext();
    }

    void ˋ(AttributeSet attributeSet, int i) {
        TypedArray obtainStyledAttributes = this.ॱˊ.obtainStyledAttributes(attributeSet, g.ˊʼ, i, 0);
        if (obtainStyledAttributes.hasValue(g.ˋʼ)) {
            this.ˋ = obtainStyledAttributes.getInt(g.ˋʼ, 0);
        }
        float dimension = obtainStyledAttributes.hasValue(g.ˋʻ) ? obtainStyledAttributes.getDimension(g.ˋʻ, SizeModifier.STABLE_STATE_SCALE) : SizeModifier.STABLE_STATE_SCALE;
        float dimension2 = obtainStyledAttributes.hasValue(g.ˌॱ) ? obtainStyledAttributes.getDimension(g.ˌॱ, SizeModifier.STABLE_STATE_SCALE) : SizeModifier.STABLE_STATE_SCALE;
        float dimension3 = obtainStyledAttributes.hasValue(g.ˋʽ) ? obtainStyledAttributes.getDimension(g.ˋʽ, SizeModifier.STABLE_STATE_SCALE) : SizeModifier.STABLE_STATE_SCALE;
        if (obtainStyledAttributes.hasValue(g.ˊʽ)) {
            int resourceId = obtainStyledAttributes.getResourceId(g.ˊʽ, 0);
            if (resourceId > 0) {
                TypedArray obtainTypedArray = obtainStyledAttributes.getResources().obtainTypedArray(resourceId);
                ˏ(obtainTypedArray);
                obtainTypedArray.recycle();
            }
        }
        obtainStyledAttributes.recycle();
        if (!ͺ()) {
            this.ˋ = 0;
        } else if (this.ˋ == 1) {
            if (!this.ʻ) {
                DisplayMetrics displayMetrics = this.ॱˊ.getResources().getDisplayMetrics();
                if (dimension2 == SizeModifier.STABLE_STATE_SCALE) {
                    dimension2 = TypedValue.applyDimension(2, 12.0f, displayMetrics);
                }
                if (dimension3 == SizeModifier.STABLE_STATE_SCALE) {
                    dimension3 = TypedValue.applyDimension(2, 112.0f, displayMetrics);
                }
                if (dimension == SizeModifier.STABLE_STATE_SCALE) {
                    dimension = 1.0f;
                }
                ॱ(dimension2, dimension3, dimension);
            }
            ʼ();
        }
    }

    @RestrictTo({d.ˊ})
    void ˊ(int i) {
        if (ͺ()) {
            switch (i) {
                case 0:
                    ʽ();
                    return;
                case 1:
                    DisplayMetrics displayMetrics = this.ॱˊ.getResources().getDisplayMetrics();
                    ॱ(TypedValue.applyDimension(2, 12.0f, displayMetrics), TypedValue.applyDimension(2, 112.0f, displayMetrics), 1.0f);
                    if (ʼ()) {
                        ᐝ();
                        return;
                    }
                    return;
                default:
                    throw new IllegalArgumentException("Unknown auto-size text type: " + i);
            }
        }
    }

    @RestrictTo({d.ˊ})
    void ˎ(int i, int i2, int i3, int i4) throws IllegalArgumentException {
        if (ͺ()) {
            DisplayMetrics displayMetrics = this.ॱˊ.getResources().getDisplayMetrics();
            ॱ(TypedValue.applyDimension(i4, (float) i, displayMetrics), TypedValue.applyDimension(i4, (float) i2, displayMetrics), TypedValue.applyDimension(i4, (float) i3, displayMetrics));
            if (ʼ()) {
                ᐝ();
            }
        }
    }

    @RestrictTo({d.ˊ})
    void ॱ(@NonNull int[] iArr, int i) throws IllegalArgumentException {
        int i2 = 0;
        if (ͺ()) {
            int length = iArr.length;
            if (length > 0) {
                int[] iArr2 = new int[length];
                if (i == 0) {
                    iArr2 = Arrays.copyOf(iArr, length);
                } else {
                    DisplayMetrics displayMetrics = this.ॱˊ.getResources().getDisplayMetrics();
                    while (i2 < length) {
                        iArr2[i2] = Math.round(TypedValue.applyDimension(i, (float) iArr[i2], displayMetrics));
                        i2++;
                    }
                }
                this.ᐝ = ˊ(iArr2);
                if (!ʻ()) {
                    throw new IllegalArgumentException("None of the preset sizes is valid: " + Arrays.toString(iArr));
                }
            }
            this.ʻ = false;
            if (ʼ()) {
                ᐝ();
            }
        }
    }

    @RestrictTo({d.ˊ})
    int ˎ() {
        return this.ˋ;
    }

    @RestrictTo({d.ˊ})
    int ॱ() {
        return Math.round(this.ॱ);
    }

    @RestrictTo({d.ˊ})
    int ˏ() {
        return Math.round(this.ʼ);
    }

    @RestrictTo({d.ˊ})
    int ˋ() {
        return Math.round(this.ॱॱ);
    }

    @RestrictTo({d.ˊ})
    int[] ˊ() {
        return this.ᐝ;
    }

    private void ˏ(TypedArray typedArray) {
        int length = typedArray.length();
        int[] iArr = new int[length];
        if (length > 0) {
            for (int i = 0; i < length; i++) {
                iArr[i] = typedArray.getDimensionPixelSize(i, -1);
            }
            this.ᐝ = ˊ(iArr);
            ʻ();
        }
    }

    private boolean ʻ() {
        int length = this.ᐝ.length;
        this.ʻ = length > 0;
        if (this.ʻ) {
            this.ˋ = 1;
            this.ʼ = (float) this.ᐝ[0];
            this.ॱॱ = (float) this.ᐝ[length - 1];
            this.ॱ = SizeModifier.STABLE_STATE_SCALE;
        }
        return this.ʻ;
    }

    private int[] ˊ(int[] iArr) {
        int size;
        if (size != 0) {
            int i;
            Arrays.sort(iArr);
            List arrayList = new ArrayList();
            for (int i2 : iArr) {
                if (i2 > 0 && Collections.binarySearch(arrayList, Integer.valueOf(i2)) < 0) {
                    arrayList.add(Integer.valueOf(i2));
                }
            }
            if (size != arrayList.size()) {
                size = arrayList.size();
                iArr = new int[size];
                for (i = 0; i < size; i++) {
                    iArr[i] = ((Integer) arrayList.get(i)).intValue();
                }
            }
        }
        return iArr;
    }

    private void ॱ(float f, float f2, float f3) throws IllegalArgumentException {
        if (f <= 0.0f) {
            throw new IllegalArgumentException("Minimum auto-size text size (" + f + "px) is less or equal to (0px)");
        } else if (f2 <= f) {
            throw new IllegalArgumentException("Maximum auto-size text size (" + f2 + "px) is less or equal to minimum auto-size " + "text size (" + f + "px)");
        } else if (f3 <= 0.0f) {
            throw new IllegalArgumentException("The auto-size step granularity (" + f3 + "px) is less or equal to (0px)");
        } else {
            this.ˋ = 1;
            this.ʼ = f;
            this.ॱॱ = f2;
            this.ॱ = f3;
            this.ʻ = false;
        }
    }

    private boolean ʼ() {
        if (ͺ() && this.ˋ == 1) {
            if (!this.ʻ || this.ᐝ.length == 0) {
                float round = (float) Math.round(this.ʼ);
                int i = 1;
                while (Math.round(this.ॱ + round) <= Math.round(this.ॱॱ)) {
                    i++;
                    round += this.ॱ;
                }
                int[] iArr = new int[i];
                float f = this.ʼ;
                for (int i2 = false; i2 < i; i2++) {
                    iArr[i2] = Math.round(f);
                    f += this.ॱ;
                }
                this.ᐝ = ˊ(iArr);
            }
            this.ˊ = true;
        } else {
            this.ˊ = false;
        }
        return this.ˊ;
    }

    @RestrictTo({d.ˊ})
    void ᐝ() {
        if (ॱॱ()) {
            if (this.ˊ) {
                if (this.ˏॱ.getMeasuredHeight() > 0 && this.ˏॱ.getMeasuredWidth() > 0) {
                    int i;
                    if (((Boolean) ˎ(this.ˏॱ, "getHorizontallyScrolling", Boolean.valueOf(false))).booleanValue()) {
                        i = 1048576;
                    } else {
                        i = (this.ˏॱ.getMeasuredWidth() - this.ˏॱ.getTotalPaddingLeft()) - this.ˏॱ.getTotalPaddingRight();
                    }
                    int height = (this.ˏॱ.getHeight() - this.ˏॱ.getCompoundPaddingBottom()) - this.ˏॱ.getCompoundPaddingTop();
                    if (i > 0 && height > 0) {
                        synchronized (ˎ) {
                            ˎ.setEmpty();
                            ˎ.right = (float) i;
                            ˎ.bottom = (float) height;
                            float ॱ = (float) ॱ(ˎ);
                            if (ॱ != this.ˏॱ.getTextSize()) {
                                ˎ(0, ॱ);
                            }
                        }
                    } else {
                        return;
                    }
                }
                return;
            }
            this.ˊ = true;
        }
    }

    private void ʽ() {
        this.ˋ = 0;
        this.ʼ = SizeModifier.STABLE_STATE_SCALE;
        this.ॱॱ = SizeModifier.STABLE_STATE_SCALE;
        this.ॱ = SizeModifier.STABLE_STATE_SCALE;
        this.ᐝ = new int[0];
        this.ˊ = false;
    }

    @RestrictTo({d.ˊ})
    void ˎ(int i, float f) {
        Resources system;
        if (this.ॱˊ == null) {
            system = Resources.getSystem();
        } else {
            system = this.ॱˊ.getResources();
        }
        ˋ(TypedValue.applyDimension(i, f, system.getDisplayMetrics()));
    }

    private void ˋ(float f) {
        if (f != this.ˏॱ.getPaint().getTextSize()) {
            boolean isInLayout;
            this.ˏॱ.getPaint().setTextSize(f);
            if (VERSION.SDK_INT >= 18) {
                isInLayout = this.ˏॱ.isInLayout();
            } else {
                isInLayout = false;
            }
            if (this.ˏॱ.getLayout() != null) {
                this.ˊ = false;
                String str = "nullLayouts";
                try {
                    Method ॱ = ॱ("nullLayouts");
                    if (ॱ != null) {
                        ॱ.invoke(this.ˏॱ, new Object[0]);
                    }
                } catch (Throwable e) {
                    Log.w("ACTVAutoSizeHelper", "Failed to invoke TextView#nullLayouts() method", e);
                }
                if (isInLayout) {
                    this.ˏॱ.forceLayout();
                } else {
                    this.ˏॱ.requestLayout();
                }
                this.ˏॱ.invalidate();
            }
        }
    }

    private int ॱ(RectF rectF) {
        int length = this.ᐝ.length;
        if (length == 0) {
            throw new IllegalStateException("No available text sizes to choose from.");
        }
        int i = 0;
        int i2 = 1;
        length--;
        while (i2 <= length) {
            i = (i2 + length) / 2;
            if (ˊ(this.ᐝ[i], rectF)) {
                int i3 = i + 1;
                i = i2;
                i2 = i3;
            } else {
                length = i - 1;
                i = length;
            }
        }
        return this.ᐝ[i];
    }

    private boolean ˊ(int i, RectF rectF) {
        StaticLayout ˊ;
        CharSequence text = this.ˏॱ.getText();
        TransformationMethod transformationMethod = this.ˏॱ.getTransformationMethod();
        if (transformationMethod != null) {
            CharSequence transformation = transformationMethod.getTransformation(text, this.ˏॱ);
            if (transformation != null) {
                text = transformation;
            }
        }
        int maxLines = VERSION.SDK_INT >= 16 ? this.ˏॱ.getMaxLines() : -1;
        if (this.ʽ == null) {
            this.ʽ = new TextPaint();
        } else {
            this.ʽ.reset();
        }
        this.ʽ.set(this.ˏॱ.getPaint());
        this.ʽ.setTextSize((float) i);
        Alignment alignment = (Alignment) ˎ(this.ˏॱ, "getLayoutAlignment", Alignment.ALIGN_NORMAL);
        if (VERSION.SDK_INT >= 23) {
            ˊ = ˊ(text, alignment, Math.round(rectF.right), maxLines);
        } else {
            ˊ = ॱ(text, alignment, Math.round(rectF.right));
        }
        if (maxLines != -1 && (ˊ.getLineCount() > maxLines || ˊ.getLineEnd(ˊ.getLineCount() - 1) != text.length())) {
            return false;
        }
        if (((float) ˊ.getHeight()) > rectF.bottom) {
            return false;
        }
        return true;
    }

    @RequiresApi(23)
    private StaticLayout ˊ(CharSequence charSequence, Alignment alignment, int i, int i2) {
        TextDirectionHeuristic textDirectionHeuristic = (TextDirectionHeuristic) ˎ(this.ˏॱ, "getTextDirectionHeuristic", TextDirectionHeuristics.FIRSTSTRONG_LTR);
        Builder hyphenationFrequency = Builder.obtain(charSequence, 0, charSequence.length(), this.ʽ, i).setAlignment(alignment).setLineSpacing(this.ˏॱ.getLineSpacingExtra(), this.ˏॱ.getLineSpacingMultiplier()).setIncludePad(this.ˏॱ.getIncludeFontPadding()).setBreakStrategy(this.ˏॱ.getBreakStrategy()).setHyphenationFrequency(this.ˏॱ.getHyphenationFrequency());
        if (i2 == -1) {
            i2 = ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED;
        }
        return hyphenationFrequency.setMaxLines(i2).setTextDirection(textDirectionHeuristic).build();
    }

    private StaticLayout ॱ(CharSequence charSequence, Alignment alignment, int i) {
        float lineSpacingMultiplier;
        float lineSpacingExtra;
        boolean includeFontPadding;
        if (VERSION.SDK_INT >= 16) {
            lineSpacingMultiplier = this.ˏॱ.getLineSpacingMultiplier();
            lineSpacingExtra = this.ˏॱ.getLineSpacingExtra();
            includeFontPadding = this.ˏॱ.getIncludeFontPadding();
        } else {
            lineSpacingMultiplier = ((Float) ˎ(this.ˏॱ, "getLineSpacingMultiplier", Float.valueOf(1.0f))).floatValue();
            lineSpacingExtra = ((Float) ˎ(this.ˏॱ, "getLineSpacingExtra", Float.valueOf(0.0f))).floatValue();
            includeFontPadding = ((Boolean) ˎ(this.ˏॱ, "getIncludeFontPadding", Boolean.valueOf(true))).booleanValue();
        }
        return new StaticLayout(charSequence, this.ʽ, i, alignment, lineSpacingMultiplier, lineSpacingExtra, includeFontPadding);
    }

    private <T> T ˎ(@NonNull Object obj, @NonNull String str, @NonNull T t) {
        try {
            t = ॱ(str).invoke(obj, new Object[0]);
            return t == null ? t : t;
        } catch (Throwable e) {
            Log.w("ACTVAutoSizeHelper", "Failed to invoke TextView#" + str + "() method", e);
            if (null != null) {
                return null;
            }
            return t;
        } catch (Throwable th) {
            if (null == null && 1 == 0) {
            }
        }
    }

    @Nullable
    private Method ॱ(@NonNull String str) {
        try {
            Method method = (Method) ˏ.get(str);
            if (method != null) {
                return method;
            }
            method = TextView.class.getDeclaredMethod(str, new Class[0]);
            if (method == null) {
                return method;
            }
            method.setAccessible(true);
            ˏ.put(str, method);
            return method;
        } catch (Throwable e) {
            Log.w("ACTVAutoSizeHelper", "Failed to retrieve TextView#" + str + "() method", e);
            return null;
        }
    }

    @RestrictTo({d.ˊ})
    boolean ॱॱ() {
        return ͺ() && this.ˋ != 0;
    }

    private boolean ͺ() {
        return !(this.ˏॱ instanceof AppCompatEditText);
    }
}
